import config from '../../utils/config.js'
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();

Page({

    data: {
                
        product_list: [],
        tab_list: [],
        TabCur: 0,
        brand_id: 0,
        brand_name: '报价',
        totalPrice: [],         // 总价格列表
        virtualList: [],        // 虚拟列表，用于渲染
        page: 1,                // 当前页
        limit: 20,              // 每页条数
        pageCount: 0,           // 总页数
        lastPage: false,        // 最后一页
        searchKey: '',			// 搜索关键词
        isSearch: false,        // 是否是搜索状态
        showLoading: false,      // 是否显示loading
    },

    onLoad: function (options) {

        const { brand_id } = options;
        const { brand_name } = options;
        this.setData({ brand_id });
        this.setData({ brand_name });
        this.getTotalPrice(brand_id);
    },

    async getTotalPrice(brand_id) {
        const totalPrice = await wxRequest.getRequest(Api.getTotalPrice(brand_id));

        if (totalPrice.data && totalPrice.errMsg == 'request:ok' && totalPrice.data.code === 200 && totalPrice.data.msg === '操作成功') {
            let tab_list = [], newTotalPrice = totalPrice.data.data;
            newTotalPrice.forEach(element => {
                tab_list = tab_list.concat(element.modelName);
            });
            const { TabCur, limit, page } = this.data;
            const pageCount = Math.ceil(newTotalPrice[TabCur].modelData.length / limit);
            const lastPage = page >= pageCount;
            this.setData({ totalPrice: newTotalPrice, tab_list, lastPage, pageCount });
            this.calcPageData();
        } else {
            console.log('获取数据时出错');
            wx.showToast({
                title: '获取数据时出错',
                icon: 'none'
            });
        }
    },

    calcPageData() {
        let { TabCur, totalPrice, virtualList, page, limit, pageCount, lastPage } = this.data;
        if (lastPage && page > pageCount) { return };
        wx.showLoading({ title: '加载中...' });
        const list = totalPrice[TabCur].modelData;
        //page-1开始下标page*limit结束下标;
        virtualList = virtualList.concat(list.slice((page - 1) * limit, page * limit));
        lastPage = page >= pageCount;
        this.setData({ virtualList, lastPage, isSearch: false });
        wx.hideLoading();
    },

    tabNavChange(e) {
        const { TabCur, perPage, totalPrice, page } = this.data;
        const pageCount = Math.ceil(totalPrice[TabCur].modelData.length / perPage);
        const lastPage = pageCount <= page;
        this.setData({ TabCur: e.detail.index, page: 1, pageCount, lastPage, virtualList: [], isSearch: false });
        this.calcPageData();
    },

    goToPart(event) {
        const { index } = event.currentTarget.dataset;
        const { virtualList } = this.data;
        const url = `/pages/carPart/carPart?price=${JSON.stringify(virtualList[index].unitPrice)}`;
        wx.navigateTo({ url });
        this.setData({ isSearch: false });
    },

    searchInput(event) {
        this.setData({ searchKey: event.detail });
    },

    searchBegin() {
        const { searchKey, totalPrice, TabCur } = this.data;
        if (!searchKey) {
            wx.showToast({ title: '未输入关键词', icon: 'none' });
            return;
        }
        const list = totalPrice[TabCur].modelData;
        let virtualList = [];
        list.forEach(element => {
            if (element.TypeName.toUpperCase().match(searchKey.toUpperCase())) {
                virtualList = virtualList.concat(element);
            }
        });
        this.setData({ virtualList, isSearch: true });
    },

    onReachBottom: function () {
        let { page, pageCount, lastPage } = this.data;

        if (lastPage || page > pageCount) {
            return;
        }
        page = page + 1;
        this.setData({ page });
        this.calcPageData();
    },

    onShareAppMessage: function () {

    }
})
